#!/bin/python
import re

def analyze():
  re_word = re.compile("Results for (.*)")
  re_score = re.compile("Score (\d+) right out of (\d+).*")
  
  try:
    results_file = open('results.txt')
  except IOError:
    return
  
  count = 0
  total_correct = 0
  total_tested = 0
  total_accuracy = 0
  
  for line in results_file:
    match_word = re_word.search(line)
    if match_word:
      word = match_word.group(1)

    match_score = re_score.search(line)
    if match_score:
      count += 1
      num_correct = int(match_score.group(1))
      num_tested = int(match_score.group(2))
      accuracy = float(num_correct)/float(num_tested)
      
      total_correct += num_correct
      total_tested += num_tested
      total_accuracy += accuracy
      
      print "%8s: %3d/%3d = %8f" % (word, num_correct, num_tested, accuracy)
    
  if total_tested != 0:
    average_accuracy = float(total_accuracy) / float(count)
    weighted_accuracy = float(total_correct) / float(total_tested)
    
    print "%17s = %f" % ("Average accuracy", average_accuracy)
    print "%17s = %f" % ("Weighted accuracy", weighted_accuracy)
  

  